package com.my.aslife.aop;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.my.aslife.module.entity.User;
import com.my.aslife.mapper.UserMapper;
import com.my.aslife.module.entity.dtos.UserLoginDto;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Slf4j
public class UpdateLoginTimeAop {

    @Autowired
    private UserMapper userMapper;

    @Around("execution(* com.my.aslife.service.impl.UserServiceImpl.login(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        UserLoginDto user = (UserLoginDto) pjp.getArgs()[0];
        // 调目标. pjp.proceed() 即表示调用目标方法
        Object result = pjp.proceed();
        // 登录成功 记录登录时间
        boolean isUpdate = userMapper.updateLoginTime(LocalDateTimeUtil.now(), user.getUserName());
        if (!isUpdate) {
            log.error("登录时间更新失败 ==> 用户名: {}", user.getUserName());
        }
        return result;
    }
}
